package cn.com.jit;

import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.expr.Name;
import com.github.javaparser.ast.expr.SimpleName;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.regex.Pattern;

public class CamelCaseChecker {

    // 检查类名是否符合大驼峰命名规范
    public static boolean isClassNameValid(String className) {
        return Pattern.matches("^[A-Z][a-zA-Z0-9]*([A-Z][a-zA-Z0-9]*)*$", className);
    }

    // 检查变量名是否符合小驼峰命名规范
    public static boolean isVariableNameValid(String variableName) {
        return Pattern.matches("^[a-z][a-zA-Z0-9]*([A-Z][a-zA-Z0-9]*)*$", variableName);
    }

    public static void checkCamelCaseInFile(String filePath) {
        try {
            String code = new String(Files.readAllBytes(new File(filePath).toPath()));
            CompilationUnit cu = StaticJavaParser.parse(code);

            new VoidVisitorAdapter<Void>() {
                @Override
                public void visit(ClassOrInterfaceDeclaration n, Void arg) {
                    super.visit(n, arg);
                    String className = n.getNameAsString();
                    if (!isClassNameValid(className)) {
                        System.out.println("Class name '" + className + "' does not follow camel case convention.");
                    }
                }

                @Override
                public void visit(FieldDeclaration n, Void arg) {
                    super.visit(n, arg);
                    List<VariableDeclarator> declarators = n.getVariables();
                    for (VariableDeclarator declarator : declarators) {
                        SimpleName name = declarator.getName();
                        String variableName = name.toString();
                        if (!isVariableNameValid(variableName)) {
                            System.out.println("Variable name '" + variableName + "' does not follow camel case convention.");
                        }
                    }
                }
            }.visit(cu, null);
        } catch (IOException e) {
            System.err.println("Error reading file: " + e.getMessage());
        } catch (Exception e) {
            System.err.println("Error parsing Java code: " + e.getMessage());
        }
    }

    public static void main(String[] args) {
        String filePath = "D:\\JIT_workspaces\\workspace\\pki508\\ra\\ra-manager\\ra-core\\src\\main\\java\\cn\\com\\jit\\pki\\ra\\dao\\AdminCertDao.java";
        checkCamelCaseInFile(filePath);
    }
}
